@echo off
setlocal enabledelayedexpansion

:: ===== 设置系统路径 =====
set "SYSTEM_PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SystemRoot%\System32\WindowsPowerShell\v1.0\"
set "PATH=%SYSTEM_PATH%%PATH%"

:: ===== 设置工作目录 =====
cd /d "%~dp0"

:: ===== 创建日志目录 =====
if not exist "Logs" mkdir "Logs"
set "LOG_FILE=Logs\tray-output-%date:~0,4%%date:~5,2%%date:~8,2%-%time:~0,2%%time:~3,2%.log"

:: ===== 记录启动信息 =====
echo [%date% %time%] Starting tray manager > "%LOG_FILE%"
echo Working directory: "%cd%" >> "%LOG_FILE%"
echo System PATH: %PATH% >> "%LOG_FILE%"

:: ===== 设置关键路径 =====
set "JAR_PATH=tray-manager.jar"
set "ICON_PATH=icon.png"

:: ===== 停止现有托盘程序 =====
echo Stopping existing tray instances... >> "%LOG_FILE%"
taskkill /f /im javaw.exe /fi "WINDOWTITLE eq ScanOS Control" >> "%LOG_FILE%" 2>&1
wmic process where "name='javaw.exe' and commandline like '%%tray-manager.jar%%'" delete >> "%LOG_FILE%" 2>&1
timeout /t 3 /nobreak >nul

:: ===== 检查JAR文件是否存在 =====
if not exist "%JAR_PATH%" (
    echo [ERROR] Tray manager JAR not found: "%JAR_PATH%" >> "%LOG_FILE%"
    echo [ERROR] Tray manager JAR not found: "%JAR_PATH%"
    goto exit_with_delay
)

if not exist "%ICON_PATH%" (
    set "ICON_PATH=icon.png"
    if not exist "%ICON_PATH%" (
        echo [WARN] Icon file not found, using default icon >> "%LOG_FILE%"
        set "ICON_PATH="
    ) else (
        echo [INFO] Using alternative icon: "%ICON_PATH%" >> "%LOG_FILE%"
    )
)

:: ===== 刷新环境变量 =====
echo Refreshing environment variables... >> "%LOG_FILE%"
set "JAVA_HOME="
for /f "tokens=2,*" %%a in ('"%SystemRoot%\System32\reg.exe" query "HKLM\SOFTWARE\ScanOS" /v JavaHome 2^>nul') do (
    set "JAVA_HOME=%%b"
    set "PATH=%%b\bin;%PATH%"
    echo Retrieved JavaHome from registry: !JAVA_HOME! >> "%LOG_FILE%"
)

:: 添加常见Java安装路径
set "PATH=%PATH%;C:\Program Files\Eclipse Adoptium\jdk-17.0.14.7-hotspot\bin"
set "PATH=%PATH%;C:\Program Files\Java\jdk-17\bin"
set "PATH=%PATH%;C:\Program Files (x86)\Eclipse Adoptium\jdk-17.0.14.7-hotspot\bin"

echo Refreshed PATH: %PATH% >> "%LOG_FILE%"

:: ===== 验证Java环境 =====
echo Checking Java environment... >> "%LOG_FILE%"

set "JAVA_CMD="
if exist "%SystemRoot%\System32\where.exe" (
    "%SystemRoot%\System32\where.exe" javaw >nul 2>&1 && set "JAVA_CMD=javaw"
    if not defined JAVA_CMD "%SystemRoot%\System32\where.exe" java >nul 2>&1 && set "JAVA_CMD=java"
)

if defined JAVA_CMD (
    echo Found Java command: %JAVA_CMD% >> "%LOG_FILE%"
) else (
    echo [ERROR] Java runtime not found >> "%LOG_FILE%"
    echo [ERROR] Java runtime not found
    
    :: 尝试直接运行java.exe作为最后手段
    if exist "%JAVA_HOME%\bin\java.exe" (
        set "JAVA_CMD="%JAVA_HOME%\bin\java""
        echo Using Java from JAVA_HOME: %JAVA_CMD% >> "%LOG_FILE%"
    ) else (
        echo [ERROR] No Java runtime available >> "%LOG_FILE%"
        echo [ERROR] No Java runtime available
        goto exit_with_delay
    )
)

:: ===== 构建启动命令 =====
set "CMD=%JAVA_CMD% -Dsun.awt.window.title=""ScanOS Control"" -jar "%JAR_PATH%""

if defined ICON_PATH (
    set "CMD=!CMD! --tray-icon="%ICON_PATH%""
)

set "CMD=!CMD! --debug --log-level=DEBUG"

:: ===== 执行命令 =====
echo Executing command: !CMD! >> "%LOG_FILE%"
echo Executing command: !CMD!

:: 关键修复：异步启动托盘程序并自动关闭窗口
start "" /B %JAVA_CMD% -Dsun.awt.window.title="ScanOS Control" -jar "%JAR_PATH%" --tray-icon="%ICON_PATH%" --debug --log-level=DEBUG

:: 检查进程是否启动
timeout /t 5 /nobreak >nul
tasklist /FI "IMAGENAME eq javaw.exe" /FI "WINDOWTITLE eq ScanOS Control" | find /i "javaw.exe" >nul
if %errorlevel% equ 0 (
    echo Tray manager started successfully >> "%LOG_FILE%"
    echo PID: !ERRORLEVEL! >> "%LOG_FILE%"
) else (
    echo [ERROR] Tray manager failed to start >> "%LOG_FILE%"
    echo [ERROR] Tray manager failed to start
)

:: 添加短暂延迟确保日志写入
timeout /t 1 /nobreak >nul

:exit
endlocal
exit /b 0

:exit_with_delay
echo Pausing for 10 seconds...
timeout /t 10 /nobreak >nul
exit /b 1